home *** CD-ROM | disk | FTP | other *** search
/ CD/PC Actual 76 / DVD Actual 1 Marzo 2003.iso / Trial / TurboCAD 7.1 Pro / Data.Cab / F24269_Eventdlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-10  |  10.2 KB  |  417 lines

  1. /******************************************************************/
  2. /*                                                                */
  3. /*                      TurboCAD for Windows                      */
  4. /*                   Copyright (c) 1993 - 1999                    */
  5. /*             International Microcomputer Software, Inc.         */
  6. /*                            (IMSI)                              */
  7. /*                      All rights reserved.                      */
  8. /*                                                                */
  9. /******************************************************************/
  10. // EventDlg.cpp : implementation file
  11. //
  12.  
  13. #include "stdafx.h"
  14. #include "Events.h"
  15. #include "EventDlg.h"
  16.  
  17. #ifdef _DEBUG
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. extern CTCEventsApp theApp;
  24.  
  25. static void DDX_EventMask(CDataExchange* pDX, UINT nIDCFirst, UINT nIDCLast, DWORD& value)
  26. {
  27.     HWND hWndDlg = pDX->m_pDlgWnd->m_hWnd;
  28.     DWORD dwBit = 1;
  29.     UINT nID;
  30.     HWND hWndButton;
  31.     if (pDX->m_bSaveAndValidate)
  32.     {
  33.         value = 0;
  34.         for (nID = nIDCFirst; nID <= nIDCLast; ++nID, dwBit <<= 1)
  35.         {
  36.             hWndButton = ::GetDlgItem(hWndDlg, nID);
  37.             if (::SendMessage(hWndButton, BM_GETCHECK, 0, 0) != 0)
  38.                 value |= dwBit;
  39.         }
  40.     }
  41.     else
  42.     {
  43.         for (nID = nIDCFirst; nID <= nIDCLast; ++nID, dwBit <<= 1)
  44.         {
  45.             hWndButton = ::GetDlgItem(hWndDlg, nID);
  46.             ::SendMessage(hWndButton, BM_SETCHECK, (value & dwBit) ? 1 : 0, 0);
  47.         }
  48.     }
  49. }
  50.  
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CAboutDlg dialog used for App About
  53.  
  54. class CAboutDlg : public CDialog
  55. {
  56. public:
  57.     CAboutDlg();
  58.  
  59. // Dialog Data
  60.     //{{AFX_DATA(CAboutDlg)
  61.     enum { IDD = IDD_ABOUTBOX };
  62.     //}}AFX_DATA
  63.  
  64.     // ClassWizard generated virtual function overrides
  65.     //{{AFX_VIRTUAL(CAboutDlg)
  66.     protected:
  67.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  68.     //}}AFX_VIRTUAL
  69.  
  70. // Implementation
  71. protected:
  72.     //{{AFX_MSG(CAboutDlg)
  73.     //}}AFX_MSG
  74.     DECLARE_MESSAGE_MAP()
  75. };
  76.  
  77. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  78. {
  79.     //{{AFX_DATA_INIT(CAboutDlg)
  80.     //}}AFX_DATA_INIT
  81. }
  82.  
  83. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  84. {
  85.     CDialog::DoDataExchange(pDX);
  86.     //{{AFX_DATA_MAP(CAboutDlg)
  87.     //}}AFX_DATA_MAP
  88. }
  89.  
  90. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  91.     //{{AFX_MSG_MAP(CAboutDlg)
  92.         // No message handlers
  93.     //}}AFX_MSG_MAP
  94. END_MESSAGE_MAP()
  95.  
  96. /////////////////////////////////////////////////////////////////////////////
  97. // CTCEventsDlg dialog
  98.  
  99. CTCEventsDlg::CTCEventsDlg(CWnd* pParent /*=NULL*/)
  100.     : CDialog(CTCEventsDlg::IDD, pParent),
  101.     m_dwEventConnection(0),
  102.     m_dwEventMask(~0UL),
  103.     m_pIApplication(NULL)
  104. {
  105.     //{{AFX_DATA_INIT(CTCEventsDlg)
  106.     //}}AFX_DATA_INIT
  107.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  108.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  109. }
  110.  
  111. // TurboCAD SDK: make sure we release the application
  112. CTCEventsDlg::~CTCEventsDlg()
  113. {
  114.     ReleaseApp();
  115. }
  116.  
  117. void CTCEventsDlg::DoDataExchange(CDataExchange* pDX)
  118. {
  119.     CDialog::DoDataExchange(pDX);
  120.     //{{AFX_DATA_MAP(CTCEventsDlg)
  121.     //}}AFX_DATA_MAP
  122.     DDX_EventMask(pDX, IDC_EVENT1, IDC_EVENT28, m_dwEventMask);
  123. }
  124.  
  125. // Public function to allow event hander to post a message on our edit box
  126.  
  127. void CTCEventsDlg::AppendText(LPCTSTR lpszText)
  128. {
  129.     int nStart, nEnd;
  130.     m_edit.SetSel(0, -1);
  131.     m_edit.GetSel(nStart, nEnd);
  132.     if (nEnd > 4000)
  133.     {
  134.         // Dump the buffer if it's too big
  135.         Clear();
  136.         nEnd = 0;
  137.     }
  138.     m_edit.SetSel(nEnd, nEnd);
  139.     m_edit.ReplaceSel(lpszText);
  140. }
  141.  
  142. // Clear our edit box
  143.  
  144. void CTCEventsDlg::Clear()
  145. {
  146.     m_edit.SetSel(0, -1);
  147.     m_edit.Clear();
  148. }
  149.  
  150. // Create an instance of the TurboCAD local server object
  151.  
  152. IUnknown* CTCEventsDlg::CreateLocalServerObject()
  153. {
  154.     // Try to create instance from TurboCAD
  155.     IUnknown* pUnk = NULL;
  156.     HRESULT hRes = ::CoCreateInstance(CLSID_Application, NULL,
  157.         CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pUnk);
  158.     if (FAILED(hRes))
  159.     {
  160.         AfxMessageBox("Could not create application object!");
  161.         return NULL;
  162.     }
  163.     return pUnk;
  164. }
  165.  
  166. // Get the running instance of the TurboCAD local server object from the ROT.
  167.  
  168. IUnknown* CTCEventsDlg::GetActiveObject(BOOL* pbCreateInstance)
  169. {
  170.     // Try to connect to running TurboCAD
  171.     IUnknown* pUnk = NULL;
  172.     HRESULT hRes = ::GetActiveObject(CLSID_Application, NULL, &pUnk);
  173.     if (SUCCEEDED(hRes))
  174.     {
  175.         // Did not create instance
  176.         *pbCreateInstance = FALSE;
  177.         return pUnk;
  178.     }
  179.     if (!*pbCreateInstance)
  180.     {
  181.         AfxMessageBox("TurboCAD is not active!");
  182.         return NULL;
  183.     }
  184.     return CreateLocalServerObject();
  185. }
  186.  
  187. // Release our pointers
  188.  
  189. void CTCEventsDlg::ReleaseApp()
  190. {
  191.     DisconnectEvents();
  192.     if (m_pIApplication != NULL)
  193.     {
  194.         m_pIApplication->Release();
  195.         m_pIApplication = NULL;
  196.     }
  197. }
  198.  
  199. // Connect our app's event sink to the TurboCAD server.
  200. // Connect or start up the TurboCAD server if we need to.
  201.  
  202. void CTCEventsDlg::ConnectEvents()
  203. {
  204.     if (m_dwEventConnection != 0)
  205.     {
  206.         AfxMessageBox("Already connected.");
  207.         return;
  208.     }
  209.     if (m_pIApplication == NULL)
  210.     {
  211.         BOOL bCreateInstance = TRUE;
  212.         IUnknown* pUnk = GetActiveObject(&bCreateInstance);
  213.         if (pUnk == NULL)
  214.             return;
  215.  
  216.         HRESULT hRes = pUnk->QueryInterface(IID_IApplication, (void**)&m_pIApplication);
  217.         pUnk->Release();
  218.         if (FAILED(hRes))
  219.         {
  220.             AfxMessageBox("Object is not a TurboCAD application!");
  221.             return;
  222.         }
  223.         else
  224.         {
  225.             if (bCreateInstance)
  226.                 AfxMessageBox("Created new Application object.");
  227.             else
  228.                 AfxMessageBox("Attached to already running Application object.");
  229.             GetDlgItem(IDC_EXIT_APP)->EnableWindow(TRUE);
  230.         }
  231.     }
  232.  
  233.     IDispatch* pUnkEventSink = theApp.GetIDispatch(TRUE);
  234.  
  235.     UpdateData(TRUE);
  236.     COleVariant var((long)m_dwEventMask);
  237.     HRESULT hRes = m_pIApplication->ConnectEvents(pUnkEventSink,
  238.         &var,
  239.         (long*)&m_dwEventConnection);
  240.     if (FAILED(hRes))
  241.     {
  242.         AfxMessageBox("Connection not established.");
  243.         return;
  244.     }
  245.     else
  246.     {
  247.         TCHAR msg[256];
  248.         wsprintf(msg, "Connection established (mask = 0x%08lx).", m_dwEventMask);
  249.         AfxMessageBox(msg);
  250.     }
  251.  
  252.     CString strButtonText;
  253.     strButtonText.LoadString(IDS_DISCONNECT);
  254.     GetDlgItem(IDC_CONNECT_EVENTS)->SetWindowText(strButtonText);
  255. }
  256.  
  257. // Release the event connection
  258.  
  259. void CTCEventsDlg::DisconnectEvents()
  260. {
  261.     if (m_pIApplication == NULL || m_dwEventConnection == 0)
  262.         return;
  263.  
  264.      HRESULT hRes = m_pIApplication->DisconnectEvents(m_dwEventConnection);
  265.     if (FAILED(hRes))
  266.     {
  267.         AfxMessageBox("Connection not broken.");
  268.         return;
  269.     }
  270.  
  271.     AfxMessageBox("Connection broken.");
  272.     m_dwEventConnection = 0;
  273.  
  274.     // DisconnectEvents can be called from destructor after DestroyWindow()...
  275.     if (::IsWindow(m_hWnd))
  276.     {
  277.         CString strButtonText;
  278.         strButtonText.LoadString(IDS_CONNECT);
  279.         GetDlgItem(IDC_CONNECT_EVENTS)->SetWindowText(strButtonText);
  280.     }
  281. }
  282.  
  283. BEGIN_MESSAGE_MAP(CTCEventsDlg, CDialog)
  284.     //{{AFX_MSG_MAP(CTCEventsDlg)
  285.     ON_WM_SYSCOMMAND()
  286.     ON_WM_PAINT()
  287.     ON_WM_QUERYDRAGICON()
  288.     ON_BN_CLICKED(IDC_CONNECT_EVENTS, OnConnectEvents)
  289.     ON_BN_CLICKED(IDC_EXIT_APP, OnExitApp)
  290.     //}}AFX_MSG_MAP
  291.     ON_CONTROL_RANGE(BN_CLICKED, IDC_EVENT1, IDC_EVENT28, OnEventMask)
  292. END_MESSAGE_MAP()
  293.  
  294. /////////////////////////////////////////////////////////////////////////////
  295. // CTCEventsDlg message handlers
  296.  
  297. // TurboCAD SDK: TurboCAD Class Identifier and Interface Identifier
  298. const CLSID CLSID_Application  = {0x6A481001,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  299. const CLSID CLSID_XApplication = {0x6A481801,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  300. const IID IID_IApplication = {0x6A481101,0xE531,0x11CF,{0xA1,0x15,0x00,0xA0,0x24,0x15,0x8D,0xAF}};
  301.  
  302. BOOL CTCEventsDlg::OnInitDialog()
  303. {
  304.     CDialog::OnInitDialog();
  305.  
  306.     // Add "About..." menu item to system menu.
  307.  
  308.     // IDM_ABOUTBOX must be in the system command range.
  309.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  310.     ASSERT(IDM_ABOUTBOX < 0xF000);
  311.  
  312.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  313.     CString strAboutMenu;
  314.     strAboutMenu.LoadString(IDS_ABOUTBOX);
  315.     if (!strAboutMenu.IsEmpty())
  316.     {
  317.         pSysMenu->AppendMenu(MF_SEPARATOR);
  318.         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  319.     }
  320.  
  321.     // Set the icon for this dialog.  The framework does this automatically
  322.     //  when the application's main window is not a dialog
  323.     SetIcon(m_hIcon, TRUE);            // Set big icon
  324.     SetIcon(m_hIcon, FALSE);        // Set small icon
  325.  
  326.     // Enable the buttons
  327.     GetDlgItem(IDC_CONNECT_EVENTS)->EnableWindow(TRUE);
  328.     GetDlgItem(IDC_EXIT_APP)->EnableWindow(FALSE);
  329.  
  330.     m_edit.SubclassDlgItem(IDC_EDIT_LOG, this);
  331.  
  332.     return TRUE;  // return TRUE  unless you set the focus to a control
  333. }
  334.  
  335. void CTCEventsDlg::OnSysCommand(UINT nID, LPARAM lParam)
  336. {
  337.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  338.     {
  339.         CAboutDlg dlgAbout;
  340.         dlgAbout.DoModal();
  341.     }
  342.     else
  343.     {
  344.         CDialog::OnSysCommand(nID, lParam);
  345.     }
  346. }
  347.  
  348. // If you add a minimize button to your dialog, you will need the code below
  349. //  to draw the icon.  For MFC applications using the document/view model,
  350. //  this is automatically done for you by the framework.
  351.  
  352. void CTCEventsDlg::OnPaint()
  353. {
  354.     if (IsIconic())
  355.     {
  356.         CPaintDC dc(this); // device context for painting
  357.  
  358.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  359.  
  360.         // Center icon in client rectangle
  361.         int cxIcon = GetSystemMetrics(SM_CXICON);
  362.         int cyIcon = GetSystemMetrics(SM_CYICON);
  363.         CRect rect;
  364.         GetClientRect(&rect);
  365.         int x = (rect.Width() - cxIcon + 1) / 2;
  366.         int y = (rect.Height() - cyIcon + 1) / 2;
  367.  
  368.         // Draw the icon
  369.         dc.DrawIcon(x, y, m_hIcon);
  370.     }
  371.     else
  372.     {
  373.         CDialog::OnPaint();
  374.     }
  375. }
  376.  
  377. // The system calls this to obtain the cursor to display while the user drags
  378. //  the minimized window.
  379. HCURSOR CTCEventsDlg::OnQueryDragIcon()
  380. {
  381.     return (HCURSOR) m_hIcon;
  382. }
  383.  
  384. void CTCEventsDlg::OnConnectEvents()
  385. {
  386.     if (m_dwEventConnection == 0)
  387.     {
  388.         ConnectEvents();
  389.     }
  390.     else
  391.     {
  392.         DisconnectEvents();
  393.     }
  394. }
  395.  
  396. void CTCEventsDlg::OnExitApp()
  397. {
  398.     if (m_pIApplication == NULL)
  399.     {
  400.         AfxMessageBox("Start application first.");
  401.         return;
  402.     }
  403.     DisconnectEvents();
  404.     m_pIApplication->Quit();
  405.     ReleaseApp();
  406.     GetDlgItem(IDC_EXIT_APP)->EnableWindow(FALSE);
  407. }
  408.  
  409. void CTCEventsDlg::OnEventMask(UINT nIDC)
  410. {
  411.     // Change the connection if we're connected...
  412.     if (m_dwEventConnection != 0)
  413.     {
  414.         DisconnectEvents();
  415.         ConnectEvents();  
  416.     }
  417. }